Introduction

As you previously discovered, the process of setting up a repository requires you to upload your work to a GitHub repository. In this exercise, you will clone the GitHub repository to your machine, commit your changes, and push your code. Completing this exercise will set you up for adding your code to the repository at the end of each exercise throughout this course.

Scenario

Rather than keeping the code and files for the Little Lemon food ordering app exclusively on your local machine, you’ll also want to maintain a version that resides online. This accomplishes two things: for one, you’ll have a backup available in case something goes wrong with the local files, and it facilitates collaboration for other developers who might join the project. Let’s find out how to set up a repository in GitHub.

Instructions

Step 1: Download and install

Begin by downloading and installing Git for your OS, as well as Architecture.

Step 2: New repository

Next, sign up via https://github.com/ and create a new repository.

Name your repository little-lemon . Use the example shown below as a reference.

You can initialize your repository with some initial files, like a README file. This is where you can write a long description for your project and detailed instructions for others to use it, which is highly recommended.

Step 3: Git terminal

Open the Git terminal on your machine.

Step 4: Copy the URL

Now, copy the GitHub repository URL by clicking the icon at the right side of the link.

Step 5: New directory

The next step is to make a new directory for the local repository. Use the following command:

mkdir capstone

Step 6: The directory

Use the following command to enter the capstone directory:

cd capstone

Step 7: Adding the URL in the Git Clone command

Use the copied URL within the Git Clone command as follows:

git clone https://github.com/yourUserName/little-lemon.git

Step 8: Verify the files

Enter the repository and check the list of files with the following command:

ls -la

This will output a list of the contents in the directory, along with such information as the owner name and the time and date of last modification. An example is provided below:

total 5

drwxr-xr-x 1 ********** 40960 Dec2 09:58 ./

drwxr-xr-x 1 ********** 40960 Dec2 09:58 ../

drwxr-xr-x 1 ********** 40960 Dec2 09:58 .git/

-rw-r--r-- 1 ********** 4096 14 Dec2 09:58 README.md

Step 9: Adding message text

Open a newfile.txt file with your favorite text editor.

Then, save the “Hello World” message text in your new file.

Step 10: Git status

Next, run the git status command to view the files.

That will produce the next output in the terminal:

On branch main

Your branch is up to date with 'origin/main'.

Untracked files:

(use "git add <file>..." to include in what will be committed)

newfile.txt

nothing added to commit but untracked files present (use "git add" to track)

Step 11: New file to stage

Add the newfile.txt to the stage with the following command:

git add newfile.txt

Step 12: Commit the changes

Now commit the changes.

git commit -m "first commit"

Step 13: Git status

Once again, check the status by running:

git status

Which produces the below output:

On branch main

Your branch is ahead of 'origin/main' by 1 commit.

(use "git push" to publish your local commits)

nothing to commit, working tree clean

Step 14: Push the changes

Now it’s time to push the changes.

git push origin main

This will output the following text on the terminal:

Enumerating objects: 4, done.

Counting objects: 100% (4/4), done.

Delta compression using up to 8 threads

Compressing objects: 100% (2/2), done.

Writing objects: 100% (3/3), 294 bytes | 294.00 KiB/s, done.

Total 3 (delta 0), reused 0 (delta 0), pack-reused 0

To https://github.com/yourUserName/little-lemon.git

7ecff08..44d2a2emain -> main

Note: You may be required to enter your GitHub username and password.

Step 15: Refresh, verify the push file and add text to commit changes

Refresh the GitHub repository page to check that the pushed file appears. Edit the file on GitHub, add some text, and commit the changes.

Step 16: Update the local repository

Go back to the Git bash shell and pull the repository to update the local repository.

git pull

The terminal will present you with a summary of what’s new compared to your previous version:

remote: Enumerating objects: 5, done.

remote: Counting objects: 100% (5/5), done.

remote: Compressing objects: 100% (2/2), done.

remote: Total 3 (delta 0), reused 0 (delta 0), pack-reused 0

Unpacking objects: 100% (3/3), 686 bytes | 14.00 KiB/s, done.

From https://github.com/yourUserName/little-lemon.git

44d2a2e..091e422main-> origin/main

Updating 44d2a2e..091e422

Fast-forward

newfile.txt | 1 +

1 file changed, 1 insertion(+)

Conclusion

In this exercise, you created a GitHub repository and cloned it as a local repository on your machine. You added a file and committed the changed status. Then, you pushed the changes back to the remote repository. After making changes remotely, you pulled it to update the local repository.